﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static Demo_MVVM基本框架.View.CsNetWorkParam;

namespace Demo_MVVM基本框架.View
{
    /// <summary>
    /// MyUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();
            DataContext = ViewModel.ViewModel.GetInstance().m_Model_MainWindow;
            string a = "abc平行线";
            Console.WriteLine(a.Contains("平行线"));
            string key = "bbbD";
            string temp = "aaaBBBcccDDD";
            bool isContains = temp.ToLower().Contains(key.ToLower());//true

            //ChoiceFun("0 SFC_OK\r\nchipid=1234\r\necid=1233\r\nasd");

            //MessageBox.Show("log", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
            //int nRet = 1;

            //var CNCStartTask = Task.Factory.StartNew(() => { MessageBox.Show(string.Format("【CNC焊接函数】第{0}个", nRet.ToString()), "CNC执行函数", MessageBoxButton.OK, MessageBoxImage.Warning); });
        }
        private void ChoiceFun(string strCompareSN)
        {

            string strIndexData = "chip3id";
            string strGet = string.Empty;
            int nIndexData = strCompareSN.IndexOf(string.Format("{0}", strIndexData));
            if (nIndexData == 0)
                strGet = "在接收内容中，未找到匹配项";
            else
            {
                int nFileB = +string.Format("{0}=", strIndexData).Length;
                int nFileE = strCompareSN.Substring(nFileB).IndexOf("\r\n");
                strGet = strCompareSN.Substring(nFileB, nFileE);
            }

        }

        public void Demo()
        {
            Process.Start(Application.ResourceAssembly.Location);
            Application.Current.Shutdown();

            //1.socket creat
            Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //2.发起建立连接的请求
            string strAddress = "127.0.0.1";
            int nPort = 7788;

            IPAddress ipaddress = IPAddress.Parse(strAddress);
            EndPoint point = new IPEndPoint(ipaddress, nPort);
            tcpClient.Connect(point);//通过IP和Port定位一个要连接道德服务器端     

            ////得到本机的MAC、IP、NET名
            //CsNetWorkParam NetWorkParameter = new CsNetWorkParam();
            //List<Network> LNetworkInformation = NetWorkParameter.GetNetworkInformation();
        }

        public void OnMsg(string strMsg)
        {
            Console.WriteLine(strMsg);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Demo();
            //ViewModel.ViewModel.GetInstance().m_Model_MainWindow.HMain2();
            ViewModel.ViewModel.GetInstance().m_Model_MainWindow.TC = "33333333333";
        }
    }
    public class CsNetWorkParam
    {
        //使用方法：声明+调用

        public struct Network
        {
            public string Name;
            public string Mac;
            public IPAddress Ip;
        };

        public List<Network> GetNetworkInformation()
        {
            //存储集合声明
            List<Network> LNetwork = new List<Network>();

            NetworkInterface[] InterfacesInformation = NetworkInterface.GetAllNetworkInterfaces();//获取网络接口所有信息 包含未启用的网卡

            foreach (NetworkInterface networkInterface in InterfacesInformation)//遍历所有网络接口信息
            {
                bool bIsOpen = networkInterface.OperationalStatus == OperationalStatus.Up;//判断网络连接状态
                bool bIsLoopback = networkInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback;//是否是 回环
                //var NetWorkType = networkInterface.NetworkInterfaceType;
                //Loopback       回环127.0.0.1
                //Wireless80211  无线网 
                //Ethernet       以太网
                if (bIsOpen && !bIsLoopback)
                {
                    //单个网卡声明
                    Network network = new Network();

                    //获取网卡名
                    network.Name = networkInterface.Name;

                    //获取ip
                    IPInterfaceProperties ipInterfaceProperties = networkInterface.GetIPProperties();//获取ip信息 包含ipv4和ipv6
                    foreach (UnicastIPAddressInformation ipAddressInformation in ipInterfaceProperties.UnicastAddresses)
                    {
                        if (ipAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)//判断是否符合ipv4
                        {
                            network.Ip = ipAddressInformation.Address;
                        }
                    }

                    //获取MAC
                    PhysicalAddress mac = networkInterface.GetPhysicalAddress();
                    string result = Regex.Replace(mac.ToString(), ".{2}", "$0:");//添加字符 .{插入位置} ${数组位置 默认0}{插入字符}
                    string strMac = result.Remove(result.Length - 1);
                    network.Mac = strMac;

                    LNetwork.Add(network);
                }
            }

            return LNetwork;
        }
    }
}
